package org.practice.dao;


import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.practice.model.Order;
import org.practice.model.User;
import org.practice.util.DaoException;
import org.practice.util.HibernateSessionFactory;
import org.practice.util.MD5Util;
import org.practice.util.UUIDUtil;

public class OrderDaoImpl implements OrderDao{
	private static Logger logger=Logger.getLogger(OrderDaoImpl.class);
	public User validateUser(String email, String password) {
		Session session=null;
		Transaction tr=null;
		session=HibernateSessionFactory.openSession();
		try {
			tr=session.beginTransaction();
			Query query=session.createQuery("from User u where u.email=? and u.password=?");
			query.setString(0, email);
			query.setString(1, MD5Util.md5(password));
			List<User> list=query.list();
			if(list.size()==1){
				return list.get(0);
			}
			tr.commit();
		} catch (HibernateException e) {
			tr.rollback();
			logger.error("数据访问错误！",e);
			new DaoException("数据访问错误！",e);
		} finally{
			HibernateSessionFactory.closeSession();
		}
		return null;
	}
	
	public void addOrder(Order order){
		Session session=null;
		Transaction tr=null;
		session=HibernateSessionFactory.openSession();
		try {
			tr=session.beginTransaction();
			order.setOrderNumber(UUIDUtil.nextCode());
			session.save(order);
			tr.commit();
		} catch (HibernateException e) {
			tr.rollback();
			logger.error("数据访问错误！",e);
			throw new DaoException("数据访问错误！",e);
		} finally{
			HibernateSessionFactory.closeSession();
		}
	}
	public User verifyUser(Integer id,String code){
		Session session=null;
		Transaction tr=null;
		try {
			session=HibernateSessionFactory.openSession();
			User user=(User)session.get(User.class, id);
			tr=session.beginTransaction();
			if(user!=null&&code.equals(user.getVerifyCode())){
				user.setVerify(true);
				tr.commit();
				return user;
			}
			
		} catch (HibernateException e) {
			tr.rollback();
			logger.error("数据访问错误！",e);
			throw new DaoException("数据访问错误！",e);
		} finally{
			HibernateSessionFactory.closeSession();
		}
		return null;
	}
	
	public User findUserById(Integer userId){
		Session session = null;
		try {
			session = HibernateSessionFactory.openSession();
			User user = (User) session.get(User.class, userId);
			return user;
		} catch (HibernateException e) {
			logger.error("数据访问错误！",e);
			throw new DaoException("数据访问错误！",e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	
	public boolean checkEmail(String email){
		Session session = null;
		try {
			session = HibernateSessionFactory.openSession();
			Query query=session.createQuery("select count(*) from User u where u.email=?");
			query.setString(0, email);
			List<Long> list=query.list();
			if(list.get(0).intValue()==0){
				return true;
			}
		} catch (HibernateException e) {
			logger.error("数据访问错误！",e);
			throw new DaoException("数据访问错误！",e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return false;
	}
}
